{% extends 'base.html' %}

{% block content %}

    <form action="" method="post">
        {% csrf_token %}
        <div class="add_article">
            <div class="alert-success text-center">添加文章</div>

            <div class="add_article_region">
                <div class="title form-group">
                    <label for="">标题</label>
                    <div>
                        <input type="text" name="title">
                    </div>
                </div>

                <div class="content form-group">
                    <label for="">内容(Kindeditor编辑器，不支持拖放/粘贴上传图片) </label>
                    <div>
                        <textarea class="content-1" name="content" id="article_content" cols="30" rows="10"></textarea>
                    </div>
                </div>
                <div>
                    <span class="span_tag">
                        <label for="">标签</label>
                        <input type="text" name="tag" id="tag">
                        <span class="tag_error"></span>
                    </span>

                    <span>
                        <label for="">分类</label>
                        <input type="text" name="category" id="category">
                        <span class="catgory_error"></span>
                    </span>


                    <br>
                    <input type="submit" class="btn btn-default" id="submit">
                </div>

            </div>


        </div>
    </form>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script charset="utf-8" src="/static/kindeditor/kindeditor-all.js"></script>

    <script>
        KindEditor.ready(function (K) {
            window.editor = K.create('#article_content', {
                width: "800",
                height: "600",
                resizeType: 2,
                uploadJson: "/upload",
                extraFileUploadParams: {
                    csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val()
                },
                filePostName: "upload_img",
                allowFileManager: true


            });
        });


        $("#submit").click(function () {
            $.ajax({
                url: "/{{ username }}/addarticle/", //请求url
                type: "post",//post
                data: {
                    "tag": $("#tag").val(),
                    "category": $("#category").val(),
                    "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val(),
                },
                success: function (data) { //回调函数
                    console.log(data)

                }
            })
        })

    </script>
{% endblock %}